package swagenda.data;

import java.io.Serializable;
import java.text.Collator;
import java.util.TreeSet;

import org.apache.log4j.Logger;

public class Category implements Serializable, Comparable<Category>
{
	private static final long serialVersionUID = 20080228;
	transient Logger logger = Logger.getLogger(this.getClass());

	public String name="";
	public TreeSet<Service> places = new TreeSet<Service>();
	public Type type;

    public enum Type
    {
    	SECTION, TAG, LOCATION;
    }

	public Category(String name, Type type)
	{
		super();
		this.name = name;
    	this.type = type;
	}

	public void addPlace(Service p) { places.add(p); }

	/**
	 * First criteria: number of places
	 * Second criteria: name alphabetically
	 */
	public int compareTo(Category c)
	{
		if (this.places.size()<c.places.size()) return 1;
		else if (this.places.size()>c.places.size()) return -1;
		else
		{
			// if same number of places order alphabetically
			return Collator.getInstance().compare(name.toLowerCase(), c.name.toLowerCase());
		}
	}

	/**
	 * Makes category ordering consistent with equals as stated in 
	 * http://java.sun.com/javase/6/docs/api/java/lang/Comparable.html
	 * Two categories are equals if they have the same number of places
	 * and the same name.
	 */
	@Override
	public boolean equals(Object o)
	{
		if (!(o instanceof Category)) return false;
		Category c = (Category) o;
		return (this.type==c.type) && (this.places.size()==c.places.size())&&(this.name.toLowerCase().equals(c.name.toLowerCase()));
	}
	
	public String toString()
	{
		return name+" ("+places.size()+")";
	}
}